package com.qiusuo0226.platform.handler;




import com.qiusuo0226.platform.annotation.valid.ValidType;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * 枚举类型参数验证器
 * @author qiusuo
 * @date 2023/6/23 16:04
 */
public class ValidTypeHandler implements ConstraintValidator<ValidType,String> {

    private List<String> typeStatus;
    /**
     * 初始化验证器
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ValidType constraintAnnotation) {
        typeStatus = Arrays.asList(constraintAnnotation.types());
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (null != value) {
            if (!typeStatus.contains(value)) {
                return false;
            }
        }
        return true;
    }
}
